/*
存档管理
*/
window._MSD = {

    _CURRENTNODE:null,

    initUi:function(node,showSave){

        //console.log('存档界面初始化....');

        _MSD._CURRENTNODE = node;

        if(showSave){
            node.getChildByName('mask').on(cc.Node.EventType.TOUCH_START,function(){
                this.active = false;
            },
            node);
        }

        var labels = node.getChildByName('label').getChildren();
        for(var i=0;i<labels.length;i++){
            var btns = labels[i].getChildByName('btns');
            //console.log(btns);
            var btnSave = btns.getChildByName('btnSave');
            var btnRead = btns.getChildByName('btnRead');
            var btnUpload = btns.getChildByName('btnUpload');
            var btnDownload = btns.getChildByName('btnDownload');

            btnSave.index = i;
            btnSave.saveUi = node;
            if(!showSave){
                btnSave.active = false;
            }
            btnSave.on(cc.Node.EventType.TOUCH_START,function(){
                //console.log('save',this.index);
                _CHOICEMANAGER.showChoice(this.saveUi,'savedata',{node:this.saveUi,index:this.index});
                //_MSD.saveData(this.index);
                //_MSD.refreshUi(this.saveUi);
            },
            btnSave);

            btnRead.index = i;
            btnRead.saveUi = node;
            btnRead.on(cc.Node.EventType.TOUCH_START,function(){
                //console.log('btnRead',this.index);
                if(_MSD.loadData(this.index)){
                    window._JUMPSENCE = 'map';
                    cc.director.loadScene('jump');
                }
            },
            btnRead);

            btnUpload.index = i;
            btnUpload.saveUi = node;
            if(!_CS.isCloud){
                btnUpload.active = false;
            }
            btnUpload.on(cc.Node.EventType.TOUCH_START,function(){
                console.log('btnUpload',this.index);
            },
            btnUpload);

            btnDownload.index = i;
            btnDownload.saveUi = node;
            if(!_CS.isCloud){
                btnDownload.active = false;
            }
            btnDownload.on(cc.Node.EventType.TOUCH_START,function(){
                console.log('btnDownload',this.index);
            },
            btnDownload);
        }

        var btnUploadAll = node.getChildByName('btnUpload');
        var btnDownloadAll = node.getChildByName('btnDownload');
        var btnLogin = node.getChildByName('btnLogin');
        var btnMain = node.getChildByName('btnMain');

        btnUploadAll.on(cc.Node.EventType.TOUCH_START,function(){
            _HTTP.uploadData();
        },btnUploadAll);

        btnDownloadAll.on(cc.Node.EventType.TOUCH_START,function(){
            _HTTP.getData();
        },btnDownloadAll);

        btnMain.on(cc.Node.EventType.TOUCH_START,function(){
            window._JUMPSENCE = "index";
            cc.director.loadScene('jump');
        },btnMain);

        btnLogin.on(cc.Node.EventType.TOUCH_START,function(){
            
            _QD.loginByQd('saveData');

        },btnDownloadAll);

        _MSD.refreshUi(node);

    },

    autoSave:function(){
        _MSD.saveData(0);
    },

    clearForSave:function(){

        for(var i=0;i<window.gameData.playerTeam.length;i++){
            window.gameData.playerTeam[i].reward = null;
        }

        for(var i=0;i<window.gameData.playerTeamBack.length;i++){
            window.gameData.playerTeamBack[i].reward = null;
        }

    },

    saveData:function(pos){
        if(_MAP&&_MAP.saveDataEnable()){
            _MSD.clearForSave();
            window.gameData.lastx=_MAP.playerNode.getPosition().x;
            window.gameData.lasty=_MAP.playerNode.getPosition().y;
            window.gameData.saveTime = _MSD.getLocalTime();
            cc.sys.localStorage.setItem(_CS.saveDataPre+pos,JSON.stringify(window.gameData));
        }else{
            _TIPS.showMessage('此处无法存档，存档需要在城墙内或者室内（解锁异能野营后可以在野外存档）');
        }
    },

    loadData:function(pos){
        var data = JSON.parse(cc.sys.localStorage.getItem(_CS.saveDataPre+pos));
        if(data!=null){
            window.gameData = data;
            return true;
        }
        return false;
    },

    //判断存档是否可继承
    ifDataPassed:function(data){
        /*
        if(data&&data.fsbpass){

            for(var key in _CMAP){

                if(
                  !data.openChapter[key]
                  &&key!='终极之战'
                ){
                    return false;
                }

            }

            return true;

        }

        return false;
        */
        //return data&&data.fsbpass;

        return data&&data.workList&&data.workList.length<=0;
    },

    getPassSaveData:function(){
        var result  = [];

        for(var i=1;i<=5;i++){

            var savedatastr = _MSD.getSaveDataStr(i);
            if(savedatastr){
                try{
                    var data = JSON.parse(savedatastr);
                    if(_MSD.ifDataPassed(data)){
                        result.push(data);
                    }
                }catch(error){
                    console.log('解析存档异常',error);
                }
            }

        }

        return result;
    },

    getSaveDataStr:function(nmb){
		if(cc.sys.localStorage.getItem(_CS.saveDataPre+nmb)){
			return cc.sys.localStorage.getItem(_CS.saveDataPre+nmb);
		}else{
			return null;
		}
	},

    saveDataStatic:function(nmb,saveData){
		cc.sys.localStorage.setItem(_CS.saveDataPre+nmb,JSON.stringify(saveData));
	},

    refreshUiCommon:function(){
        _MSD.refreshUi(_MSD._CURRENTNODE);
    },

    refreshUi:function(node){
        
        var labels = node.getChildByName('label').getChildren();
        var btnUpload = node.getChildByName('btnUpload');
        var btnDownload = node.getChildByName('btnDownload');
        var btnLogin = node.getChildByName('btnLogin');
        for(var i=0;i<labels.length;i++){
            var title = labels[i].getChildByName('title');
            title.getComponent(cc.Label).string = _MSD.getDataTitle(i);
        }

        if(_HQM._USERNAME){
            btnUpload.active = true;
            btnDownload.active = true;
            btnLogin.active = false;
        }else{
            btnUpload.active = false;
            btnDownload.active = false;
            btnLogin.active = true;
        }
    },

    getDataTitLeByData:function(data,pos){

        var title = pos+".";

        if(
            data.playerTeam
            &&data.playerTeam[0]
            &&data.playerTeam[0].name
        ){
            title+=data.playerTeam[0].name+" ";
        }
        title+=data.saveTime;
        
        if(_MSD.ifDataPassed(data)){
            title+=" "+_MCH.getHardNameByData(data)+"通关";
        }else{
            title+=" "+_MCH.getHardNameByData(data);
        }

        if(data.passType&&_PLAYERSTATUS._NAME[data.passType]){
            title+=" ["+_PLAYERSTATUS._NAME[data.passType]+"]";
        }
        return title;

    },

    getDataTitle:function(pos){
        if(cc.sys.localStorage.getItem(_CS.saveDataPre+pos)){
            var data = JSON.parse(cc.sys.localStorage.getItem(_CS.saveDataPre+pos));
            if(data!=null){
                
                var title = pos==0?"自动档(不会上传).":pos+".";

                if(
                    data.playerTeam
                    &&data.playerTeam[0]
                    &&data.playerTeam[0].name
                ){
                    title+=data.playerTeam[0].name+" ";
                }
                
                title+=data.saveTime;
                
                if(_MSD.ifDataPassed(data)){
                    title+=" "+_MCH.getHardNameByData(data)+"通关";
                }else{
                    title+=" "+_MCH.getHardNameByData(data);
                }

                if(data.passType&&_PLAYERSTATUS._NAME[data.passType]){
                    title+=" ["+_PLAYERSTATUS._NAME[data.passType]+"]";
                }
                return title;
            }
        }
        return pos+'.空';
    },

    getLocalTime:function(){
		var testDate = new Date();
		return testDate.getFullYear()+"-"+(testDate.getMonth()+1)+"-"+testDate.getDate()+" "+testDate.getHours()+":"+testDate.getMinutes()+":"+testDate.getSeconds();
	},


}